﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Yipusoft.Exercitation.DBUtility;
using Yipusoft.Exercitation.BLL;
using Yipusoft.Exercitation.Model;
using Yipusoft.Common.BLL;

public partial class Combat_InputStat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            InitData();
    }

    private void InitData()
    {
        for (int i = 2010; i <= 2050; i++)
        { 
            ddlYear.Items.Add(new ListItem(i.ToString()+"年",i.ToString()));
        }
        for(int i=1;i<=12;i++)
        {
            ddlMonth.Items.Add(new ListItem(i.ToString()+"月",i.ToString()));
        }
     

        ddlStation.DataSource = CodeItem.GetList(" ParentID='SBGW0000'");
        ddlStation.DataTextField = "itemName";
        ddlStation.DataValueField = "ItemID";
        ddlStation.DataBind();

        ddlStation.Items.Insert(0, new ListItem("全部", "0000"));
        ddlRank.DataSource = CodeItem.GetList(" ParentID='ZXSBJX00'");
        ddlRank.DataTextField = "ItemName";
        ddlRank.DataValueField = "ItemID";
        ddlRank.DataBind();
        ddlRank.Items.Insert(0, new ListItem("全部", "0000"));
    }

    private void Bind()
    {
       List<Yipusoft.Account.Model.DepartmentInfo> departmentList=  Yipusoft.Account.BLL.Department.GetList("Right(DepartmentID,3)='000' And DepartmentID<>'1300000' And Left(DepartmentID,4)<>'1314' And Left(DepartmentID,4)<>'1315'");
       DataTable dtb = new DataTable();
       DataColumn clm;
       DataRow drw;
       clm = new DataColumn("DepartmentID", typeof(System.String));
       dtb.Columns.Add(clm);
       clm = new DataColumn("DepartmentName", typeof(System.String));
       dtb.Columns.Add(clm);
       clm = new DataColumn("InputScore", typeof(System.Decimal)); //录入率
       dtb.Columns.Add(clm);
       clm = new DataColumn("Ach", typeof(System.Decimal));//达标率
       dtb.Columns.Add(clm);
       clm = new DataColumn("SumAch", typeof(System.Decimal));//总达标率
       dtb.Columns.Add(clm);

       foreach (Yipusoft.Account.Model.DepartmentInfo dpt in departmentList)
       {
           drw = dtb.NewRow();
           drw["DepartmentID"] = dpt.DepartmentID;
           drw["DepartmentName"] = dpt.ShortName;
           drw["InputScore"] = GetInputScore(dpt.DepartmentID);
           drw["Ach"] = GetAchScore(dpt.DepartmentID);
           drw["SumAch"] = GetSumScore(dpt.DepartmentID);
           dtb.Rows.Add(drw);

       }
       UserChart.Series["录入率"].Points.DataBindXY(dtb.DefaultView, "DepartmentName", dtb.DefaultView, "InputScore");
       UserChart.Series["达标率"].Points.DataBindXY(dtb.DefaultView, "DepartmentName", dtb.DefaultView, "Ach");
       UserChart.Series["总达标率"].Points.DataBindXY(dtb.DefaultView, "DepartmentName", dtb.DefaultView, "SumAch");

       for (int i = 0; i < dtb.Rows.Count; i++)
       {
           UserChart.Series["录入率"].Points[i].Href = "SquadronStat.aspx?DepartmentID="+dtb.Rows[i][0].ToString()+"&Year="+ddlYear.SelectedValue+"&Month="+ddlMonth.SelectedValue+"&StationID="+ddlStation.SelectedValue+"&RankID="+ddlRank.SelectedValue;
           UserChart.Series["达标率"].Points[i].Href = "SquadronStat.aspx?DepartmentID=" + dtb.Rows[i][0].ToString() + "&Year=" + ddlYear.SelectedValue + "&Month=" + ddlMonth.SelectedValue + "&StationID=" + ddlStation.SelectedValue + "&RankID=" + ddlRank.SelectedValue; ;
           UserChart.Series["总达标率"].Points[i].Href = "SquadronStat.aspx?DepartmentID=" + dtb.Rows[i][0].ToString() + "&Year=" + ddlYear.SelectedValue + "&Month=" + ddlMonth.SelectedValue + "&StationID=" + ddlStation.SelectedValue + "&RankID=" + ddlRank.SelectedValue; ;
       }
    }

    public string GetTaskID()
    {
       TaskBaseInfo taskInfo= TaskBase.GetList(" Year(EndDate)=" + ddlYear.SelectedValue + " And Month(EndDate)=" + ddlMonth.SelectedValue + "").FirstOrDefault();
       return taskInfo != null ? taskInfo.TaskID.ToString() : Guid.Empty.ToString();
    }
    public string GetRankAndStation()
    {
        string where = string.Empty;
        if(!ddlRank.SelectedValue.Equals("0000"))
        {
            where = " And RankID='" + ddlRank.SelectedValue + "'";
        }
        if (!ddlStation.SelectedValue.Equals("0000"))
        {
            where += " And StationID='" + ddlStation.SelectedValue + "'";
        }

        return where;
    }

    //录入率
    public decimal GetInputScore(string departmentID)
    { 
        string where=GetRankAndStation();
       //应录入人数
        string sql="Select Count(*) From (Select SoldierID From TaskSoldiers Where Left(DepartmentID,4)='"+departmentID.Substring(0,4)+"'"+where+" And TaskID='"+GetTaskID()+"' Group by SoldierID) ##t";
        decimal count = decimal.Parse(SqlHelper.ExecuteScalar(SqlHelper.CONN_STRING, CommandType.Text, sql).ToString());

        //已录入人数
        sql = "Select Count(*) From (Select SoldierID From TaskSoldierScore Where   Left(DepartmentID,4)='" + departmentID.Substring(0, 4) + "' And  TaskID='" + GetTaskID() + "' And  SoldierID IN (Select SoldierID From TaskSoldiers Where Left(DepartmentID,4)='" + departmentID.Substring(0, 4) + "'" + where + " And TaskID='" + GetTaskID() + "')   Group by SoldierID) ##t1";
        //sql = "Select Count(*) From (Select SoldierID From TaskSoldierScore Where  SoldierID IN (Select SoldierID From TaskSoldiers Where Left(DepartmentID,4)='" + departmentID.Substring(0, 4) + "'" + where + " And TaskID='" + GetTaskID() + "')   Group by SoldierID) ##t1";
        decimal count2 = decimal.Parse(SqlHelper.ExecuteScalar(SqlHelper.CONN_STRING, CommandType.Text, sql).ToString());

        if (count > 0)
            return (count2 / count)*100;
        else
            return 0;
    }
    //达标率
    public decimal GetAchScore(string departmentID)
    {
        string where = GetRankAndStation();
        //达标人数
        string sql = "Select Count(*) From	(Select SoldierID From TaskSoldierScore Where    Left(DepartmentID,4)='" + departmentID.Substring(0, 4) + "' And  TaskID='" + GetTaskID() + "' And  Score=1 And SoldierID IN (Select SoldierID From TaskSoldiers Where Left(DepartmentID,4)='" + departmentID.Substring(0, 4) + "'" + where + " And TaskID='" + GetTaskID() + "')  Group by SoldierID) ##t3";
        decimal count = decimal.Parse(SqlHelper.ExecuteScalar(SqlHelper.CONN_STRING, CommandType.Text, sql).ToString());

        //已录入人数
        sql = "Select Count(*) From	(Select SoldierID From TaskSoldierScore Where    Left(DepartmentID,4)='" + departmentID.Substring(0, 4) + "' And  TaskID='" + GetTaskID() + "' And   SoldierID IN (Select SoldierID From TaskSoldiers Where Left(DepartmentID,4)='" + departmentID.Substring(0, 4) + "'" + where + " And TaskID='" + GetTaskID() + "')  Group by SoldierID) ##t4";
        decimal count2 = decimal.Parse(SqlHelper.ExecuteScalar(SqlHelper.CONN_STRING, CommandType.Text, sql).ToString());

        if (count > 0)
            return (count / count2)*100;
        else
            return 0;
    }

    public decimal GetSumScore(string departmentID)
    {
        string where = GetRankAndStation();
        //达标人数
        string sql = "Select Count(*) From	(Select SoldierID From TaskSoldierScore Where    Left(DepartmentID,4)='" + departmentID.Substring(0, 4) + "' And  TaskID='" + GetTaskID() + "' And  Score=1 And SoldierID IN (Select SoldierID From TaskSoldiers Where Left(DepartmentID,4)='" + departmentID.Substring(0, 4) + "'" + where + " And TaskID='" + GetTaskID() + "')  Group by SoldierID) ##t5";
        decimal count = decimal.Parse(SqlHelper.ExecuteScalar(SqlHelper.CONN_STRING, CommandType.Text, sql).ToString());

        //应录入人数
        sql = "Select Count(*) From (Select SoldierID From TaskSoldiers Where Left(DepartmentID,4)='" + departmentID.Substring(0, 4) + "'" + where + " And TaskID='" + GetTaskID() + "' Group by SoldierID) ##t6";
        decimal count2 = decimal.Parse(SqlHelper.ExecuteScalar(SqlHelper.CONN_STRING, CommandType.Text, sql).ToString());

        if (count > 0)
            return (count / count2) * 100;
        else
            return 0;
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        Bind();
    }
  

}
